package com.lysj.bhp.supernode.service;

import com.lysj.bhp.api.base.ApiContent;
import com.lysj.bhp.supernode.base.SuperNodeContent;
import com.lysj.bhp.supernode.domain.SuperNode;
import com.lysj.bhp.supernode.domain.SuperNodeUser;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

@Service
public class ApiSuperNodeService extends SuperNodeContent {


    @Transactional
    public Resp addNodeUser(String superNodeCode,String mineAccountEveryOne) {

        if (ParamUtil.isBlack(superNodeCode)) {
            return new Resp(Resp.PARAM_ERROR, "请输入超级编码");
        }
        //根据输入的编码,检测超级节点是否存在
        SuperNode superNode = superNodeRepository.findBySuperNodeCode(superNodeCode);
        if (null == superNode) {
            return new Resp(Resp.PARAM_ERROR, "超级节点编码错误");
        }
        //判定用户之前是否申请过超级节点
        SuperNodeUser superNodeUserQuery = superNodeUserRepository.findByUserId(currentUser().getUserId());
        if (null != superNodeUserQuery) {
            return new Resp(Resp.PARAM_ERROR, "您已申请过超级节点,请勿重复添加");
        }

        //超级节点表用户数量+1
        superNode.setUserNumber(superNode.getUserNumber() + 1);
        superNodeRepository.save(superNode);

        //创建超级节点用户表
        SuperNodeUser superNodeUser = new SuperNodeUser();
        superNodeUser.setUserId(currentUser().getUserId());
        superNodeUser.setUserEmail(currentUser().getEmail());
        superNodeUser.setSuperNodeId(superNode.getSuperNodeId());
        superNodeUser.setSuperNodeCode(superNode.getSuperNodeCode());
        superNodeUser.setCreateDate(new Date());
        superNodeUser.setMineAccountEveryOne(mineAccountEveryOne);
        superNodeUserRepository.save(superNodeUser);
        return new Resp("添加超级节点成功");
    }
}
